
#
# Module dependencies.
#

express = require('express')
routes = require('routes')
{Refresh} = require ('./refresh')
{MongoRepository} = require ('./mongoRepository')
{RefreshReturnValue} = require ('./refreshReturnValue')
app = module.exports = express.createServer()

mongoServerName = 'localhost'
databaseName = 'NodeTest'

# Configuration

app.configure  ->
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use app.router
  app.use express.static (__dirname + '/public')

app.configure 'development', ->
  app.use express.errorHandler({dumpExceptions: true, showStack: true})

app.configure 'production', ->
  app.use express.errorHandler()

# Routes
app.get '/', (req, res) ->
    res.send 'Hello World'

# Routes
app.get '/refresh', (req, res) ->
    res.send 'Hello World'

app.param '/', (req, res) ->
  console.log "in slash handler"

app.get '/refresh/:RefreshRequest', (req, res) ->
  apiKeyRepository = new MongoRepository(mongoServerName, databaseName, "ApiKey")
  tokenRepository = new MongoRepository(mongoServerName, databaseName, "Token")
  refresh = new Refresh(apiKeyRepository, tokenRepository, null)
  refresh.refresh req.params.RefreshRequest, (err, token) ->
    if not err
      refreshReturnValue = new RefreshReturnValue()
      refreshReturnValue.AccessToken = token.Id
      refreshReturnValue.AccessTokenExpiration = token.Expiration
      res.json(refreshReturnValue)
    else
      res.json 400, err.message
app.listen process.env.port || 3000

#console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)